import axios from 'axios'
import { Message } from 'element-ui'
import Cookies from 'js-cookie'
import store from '@/store'
import qs from 'qs'
// import { getToken } from '@/utils/auth'
// create an axios instance
const service = axios.create({
  baseURL: 'process.env.BASE_API', // api 的 base_url
  timeout: 120000 // request timeout
})
// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    // if (store.getters.token) {
    //   // 让每个请求携带token-- ['X-Token']为自定义key
    //   config.headers['X-Token'] = getToken()
    // }
    // 替换域名，动态设置
    const { url } = config
    // console.log("防静电很快就的说法",qs.parse(location.href.split('?')[1]).h_aid)
    // console.log("123fhdshfdj福袋后己方的",config)
    if (config.url.includes('/')) {
      const requerBase = url.substr(1, url.length - 1)
      config.url = `${Cookies.get('BASE_API') || `${process.env.BASE_API}/`}${requerBase}`
      //处理代理，跳转代理之后每个接口都要带aid
    let h_aid = qs.parse(location.href.split('?')[1]).h_aid
    let congdata = config.data
      if(h_aid){
        if(congdata == '' || congdata=='undefined'){
         config.data = qs.stringify({h_aid:h_aid})
        }
      }
    }
    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    // const res = response.data
    // if (res.code !== 20000) {
    //   Message({
    //     message: res.message,
    //     type: 'error',
    //     duration: 5 * 1000
    //   })
    //   // 50008:非法的token; 50012:其他客户端登录了;  50014:Token 过期了;
    //   if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
    //     // 请自行在引入 MessageBox
    //     // import { Message, MessageBox } from 'element-ui'
    //     MessageBox.confirm('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出', {
    //       confirmButtonText: '重新登录',
    //       cancelButtonText: '取消',
    //       type: 'warning'
    //     }).then(() => {
    //       store.dispatch('FedLogOut').then(() => {
    //         location.reload() // 为了重新实例化vue-router对象 避免bug
    //       })
    //     })
    //   }
    //   return Promise.reject('error')
    // } else {
    //   return response.data
    // }

    const res = response.data
    if (res.code !== 1) {
      Message({
        message: res.msg,
        type: 'error',
        duration: 5 * 1000
      })
      if (res.code === 302) {
        store.dispatch('FedLogOut').then(() => {
          const outPath = Cookies.get('LOGOUT')
          if (outPath) {
            location.href = outPath
          } else {
            location.href = `${location.protocol}//${location.hostname}`
            // location.reload() // 为了重新实例化vue-router对象 避免bug
            // location.href = `https://devwe7.weliam.cn/weliam.php`
          }
        })
      }
      return Promise.reject(res.msg)
    } else {
      // Message({
      //   message: res.msg || 'success',
      //   type: 'success',
      //   duration: 5 * 1000
      // })
      return response
    }
  },
  error => {
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
